package com.csnf.servlet;

import com.csnf.entity.Singer;
import com.csnf.service.SingerService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.datatransfer.StringSelection;
import java.io.IOException;
import java.util.List;

@WebServlet("/singer")
public class SingerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }
    SingerService service = new SingerService();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        //获取type值
        String type = req.getParameter("type");
        //根据type值判断执行哪个操作
        //type.equals("all")
        if ("all".equals(type)) {
            String singerName = req.getParameter("singerName");
            String sex = req.getParameter("sex");
            String piaoshu1 = req.getParameter("piaoshu1");
            String piaoshu2 = req.getParameter("piaoshu2");
            String page=req.getParameter("page");//当前第几页
            if(null==page || "".equals(page)){
                page="1";
            }
            String y ="1";//每页显示的条数
            String x =((Integer.valueOf(page)-1)*Integer.valueOf(y))+"";
            List<Singer> slist = service.getSinger(singerName, sex, piaoshu1, piaoshu2,x,y);
            System.out.println(slist);
            int allcoun=service.getSingerCount();

            int allPage=0;
            if (allcoun % Integer.valueOf(y)==0){
                allPage=allcoun/Integer.valueOf(y);
            }else {
                allPage=allcoun/Integer.valueOf(y)+1;
            }
            req.setAttribute("allPage",allPage);
            req.setAttribute("page",page);
            //将歌手信息保存到list中
            req.setAttribute("sList", slist);
            //进入index.jsp页面
            req.getRequestDispatcher("index.jsp").forward(req, resp);

        }else if ("toupiao".equals(type)){
            String sId =req.getParameter("sId");
            System.out.println("sId="+sId);
            int i = service.updateToupiao(sId);
            if (i>0){
                req.setAttribute("msg","1");

            }else {
                req.setAttribute("msg","0");
            }
            req.getRequestDispatcher("singer?type=all").forward(req, resp);
        }
    }
}
